package service

import (
	"ksd-social-api/commons/page"
	cmodel "ksd-social-api/modules/note/model"
	cvo "ksd-social-api/modules/note/model/vo"
	"ksd-social-api/modules/user/model/context"
	"ksd-social-api/utils"
)

/**
 * 我的小册
 * @author feige
 * @date 2023-12-14
 * @version 1.0
 * @desc
 */
func (service *UserCenterService) FindMyNotePage(ctx context.UserCenterContext) *page.Page {
	notePage, _ := noteMapper.FindMyNotePage(ctx.UserId, ctx.ChannelId, ctx.SystemId, ctx.PageNo, ctx.PageSize)
	noteList := notePage.Records.([]cmodel.Note)
	if noteList != nil {
		noteDetailVos := []cvo.NoteDetailVo{}
		countNoteStudys := noteMapper.CountNoteStudys(ctx.UserId, ctx.SystemId)
		for _, note := range noteList {
			noteVo := cvo.NoteDetailVo{}
			utils.CopyProperties(&noteVo, note)
			for _, study := range countNoteStudys {
				if study.NoteId == note.Id {
					noteVo.Percent = study.Percent
				}
			}
			noteDetailVos = append(noteDetailVos, noteVo)
		}
		notePage.Records = noteDetailVos
	}
	return notePage
}

/**
 * 我的浏览记录小册
 * @author feige
 * @date 2023-12-14
 * @version 1.0
 * @desc
 */
func (service *UserCenterService) FindMyNoteBroswerPage(ctx context.UserCenterContext) *page.Page {
	notePage, _ := noteMapper.FindMyNoteBroswerPage(ctx.UserId, ctx.ChannelId, ctx.SystemId, ctx.PageNo, ctx.PageSize)
	noteList := notePage.Records.([]cmodel.Note)
	if noteList != nil {
		noteDetailVos := []cvo.NoteDetailVo{}
		for _, note := range noteList {
			noteVo := cvo.NoteDetailVo{}
			utils.CopyProperties(&noteVo, note)
			noteDetailVos = append(noteDetailVos, noteVo)
		}
		notePage.Records = noteDetailVos
	}
	return notePage
}

/**
 * 收藏的小册
 * @author feige
 * @date 2023-12-14
 * @version 1.0
 * @desc
 */
func (service *UserCenterService) FindNoteUserFavPage(ctx context.UserCenterContext) *page.Page {
	notePage, _ := noteUserFavMapper.FindNoteUserFavPage(ctx.UserId, ctx.ChannelId, ctx.SystemId, ctx.PageNo, ctx.PageSize)
	noteList := notePage.Records.([]cmodel.Note)
	if noteList != nil {
		noteDetailVos := []cvo.NoteDetailVo{}
		for _, note := range noteList {
			noteVo := cvo.NoteDetailVo{}
			utils.CopyProperties(&noteVo, note)
			noteDetailVos = append(noteDetailVos, noteVo)
		}
		notePage.Records = noteDetailVos
	}
	return notePage
}

/**
 * 喜欢的小册
 * @author feige
 * @date 2023-12-14
 * @version 1.0
 * @desc
 */
func (service *UserCenterService) FindNoteUserLikePage(ctx context.UserCenterContext) *page.Page {
	notePage, _ := noteUserLikeMapper.FindNoteUserLikePage(ctx.UserId, ctx.ChannelId, ctx.SystemId, ctx.PageNo, ctx.PageSize)
	noteList := notePage.Records.([]cmodel.Note)
	if noteList != nil {
		noteDetailVos := []cvo.NoteDetailVo{}
		for _, note := range noteList {
			noteVo := cvo.NoteDetailVo{}
			utils.CopyProperties(&noteVo, note)
			noteDetailVos = append(noteDetailVos, noteVo)
		}
		notePage.Records = noteDetailVos
	}
	return notePage
}

/**
 * 取消小册收藏
 * @author feige
 * @date 2024-01-13
 * @version 1.0
 * @desc
 */
func (service *UserCenterService) CancelFavNote(userId uint64, noteId uint64, systemId uint64) bool {
	return noteUserFavMapper.CancelNoteUserFav(userId, noteId, systemId)
}

/**
 * 取消小册点赞
 * @author feige
 * @date 2024-01-13
 * @version 1.0
 * @desc
 */
func (service *UserCenterService) CanceLikeNote(userId uint64, noteId uint64, systemId uint64) bool {
	return noteUserLikeMapper.CancelNoteUserLike(userId, noteId, systemId)
}

/**
 * 删除小册浏览记录
 * @author feige
 * @date 2024-01-13
 * @version 1.0
 * @desc
 */
func (service *UserCenterService) RemoveNoteHits(userId uint64, noteId uint64, systemId uint64) bool {
	return noteMapper.RemoveNoteHits(userId, noteId, systemId)
}
